package com.javaweb.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.javaweb.api.dto.LoginDto;
import com.javaweb.api.dto.UpdatePwdDto;
import com.javaweb.api.dto.UpdateUserInfoDto;
import com.javaweb.api.entity.Member;
import com.javaweb.api.entity.MemberGroup;
import com.javaweb.api.entity.MemberMenu;
import com.javaweb.api.mapper.MemberGroupMapper;
import com.javaweb.api.mapper.MemberMapper;
import com.javaweb.api.service.IMemberMenuService;
import com.javaweb.api.service.IMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.javaweb.api.utils.ShiroUtils;
import com.javaweb.api.vo.member.MemberInfoVo;
import com.javaweb.common.utils.*;
import io.jsonwebtoken.Claims;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

import static com.javaweb.common.utils.JwtUtil.parseJWT;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author 鲲鹏
 * @since 2021-10-12
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements IMemberService {

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private MemberGroupMapper memberGroupMapper;


    @Autowired
    private IMemberMenuService memberMenuService;


    /**
     * 获取用户信息
     *
     * @return
     */
    @Override
    public JsonResult getUserInfo() {
        Integer memberId = ShiroUtils.getUserId();
        Member member = memberMapper.selectById(memberId);

        // 拷贝属性
        MemberInfoVo userInfoVo = new MemberInfoVo();
        BeanUtils.copyProperties(member, userInfoVo);
        // 会员头像
        if (StringUtils.isNotNull(member.getAvatar())) {
            userInfoVo.setAvatar(CommonUtils.getImageURL(member.getAvatar()));
        }

        // 获取权限菜单
        List<MemberMenu> menuList = memberMenuService.getMemberMenuList(member.getMemberGroup());
        userInfoVo.setAuthorities(menuList);

        // 获取节点权限
        List<String> permissionList = memberMenuService.getPermissionList(member.getMemberGroup());
        userInfoVo.setPermissionList(permissionList);

        //所属用户组名称
        MemberGroup memberGroup = memberGroupMapper.selectById(member.getMemberGroup());
        userInfoVo.setMemberGroupName(memberGroup.getName());

        return JsonResult.success(userInfoVo);
    }

    /**
     * 修改密码
     *
     * @param updatePwdDto 参数
     * @return
     */
    @Override
    public JsonResult updatePwd(UpdatePwdDto updatePwdDto) {
        Member member = memberMapper.selectById(ShiroUtils.getUserId());
        if (member == null) {
            return JsonResult.error("用户信息不存在");
        }
        // 校验旧密码是否正确
        if (!CommonUtils.password(updatePwdDto.getOldPassword()).equals(member.getPassword())) {
            return JsonResult.error("原始密码不正确");
        }

        // 设置新密码
        member.setPassword(CommonUtils.password(updatePwdDto.getNewPassword()));
        member.setSalt(CommonUtils.getRandomStr(false, 6));
        Integer result = memberMapper.updateById(member);
        if (result == 0) {
            return JsonResult.error("修改密码失败");
        }
        return JsonResult.success();
    }


    /**
     * 更新个人资料
     *
     * @param updateUserInfoDto 参数
     * @return
     */
    @Override
    public JsonResult updateUserInfo(UpdateUserInfoDto updateUserInfoDto) {
        // 用户ID验证
        if (StringUtils.isNull(ShiroUtils.getUserId()) || ShiroUtils.getUserId() <= 0) {
            return JsonResult.error("用户ID不能为空");
        }

        // 实例化用户对象
        Member member = new Member();

        if (updateUserInfoDto.getAvatar()!=null&&
                updateUserInfoDto.getAvatar().startsWith("data")) {
            BeanUtils.copyProperties(updateUserInfoDto, member);
            // 头像Base64转换处理
            String avatar = Base64Utils.saveImg(updateUserInfoDto.getAvatar(), "member");
            member.setAvatar(avatar);
        } else {
            BeanUtils.copyProperties(updateUserInfoDto, member, "avatar");
        }
        member.setId(ShiroUtils.getUserId());

        int result = memberMapper.updateById(member);
        if (result == 0) {
            return JsonResult.error("更新失败");
        }
        return JsonResult.success("更新成功");
    }
}
